package com.sg.service.base.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.finance.req.QueryAccountSubjectDetailReq;
import com.sg.dto.base.finance.req.QueryAccountSubjectListReq;
import com.sg.entity.OmsAccountSubject;
import com.sg.service.base.finance.MOmsAccountSubjectService;
import com.wicket.okrfinance.common.dto.FieldInfo;
import com.wicket.okrfinance.common.exception.BizException;
import com.wicket.okrfinance.dal.mapper.mbg.OmsAccountSubjectMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAccountSubjectServiceImpl
        extends ServiceImpl<OmsAccountSubjectMapper, OmsAccountSubject>
        implements MOmsAccountSubjectService {

    @Autowired
    OmsAccountSubjectMapper omsAccountSubjectMapper;

    /**
     * 7-1-06查询财务科目详情[7710]
     * gen by moon at 11/7/2023, 4:40:20 PM
     */
    @Trace(operationName = "7-1-06查询财务科目详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAccountSubject queryAccountSubjectDetail(QueryAccountSubjectDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsAccountSubject> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAccountSubject> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAccountSubject retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 7-1-06查询财务科目列表[7711]
     * gen by moon at 11/7/2023, 4:40:20 PM
     */
    @Trace(operationName = "7-1-06查询财务科目列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAccountSubject> queryAccountSubjectList(QueryAccountSubjectListReq reqDto) {

        try {
            QueryWrapper<OmsAccountSubject> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAccountSubject.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAccountSubject.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAccountSubject.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAccountSubject>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 7-1-06新增财务科目[8005]
     * gen by moon at 2/24/2024, 12:30:03 AM
     */
    @Trace(operationName = "7-1-06新增财务科目")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAccountSubject(OmsAccountSubject omsAccountSubject) {
        omsAccountSubject.setAccountSubjectld(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAccountSubject.setAccountSubjectCode(CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsAccountSubject.setIsArchive("FALSE");
        omsAccountSubject.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAccountSubject.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAccountSubject != null && omsAccountSubject.getSubjectLifeCycle() != null && omsAccountSubject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAccountSubject.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAccountSubject.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAccountSubject.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAccountSubject != null && omsAccountSubject.getSubjectLifeCycle() != null && omsAccountSubject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAccountSubject.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAccountSubject.setIsValid("FALSE");
        omsAccountSubject.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsAccountSubject.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsAccountSubject)) {
            return omsAccountSubject.getAccountSubjectld();
        } else {
            return "-1";
        }

    }

    /**
     * 7-1-06删除财务科目[8009]
     * gen by moon at 11/24/2023, 1:04:11 AM
     */
    @Trace(operationName = "7-1-06删除财务科目")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAccountSubject(String accountSubjectld) {

        return super.removeById(accountSubjectld);
    }

    /**
     * 7-1-06修改财务科目[8015]
     * gen by moon at 11/24/2023, 1:04:12 AM
     */
    @Trace(operationName = "7-1-06修改财务科目")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAccountSubject(OmsAccountSubject omsAccountSubject) {
        omsAccountSubject.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAccountSubject != null && omsAccountSubject.getSubjectLifeCycle() != null && omsAccountSubject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAccountSubject.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAccountSubject != null && omsAccountSubject.getSubjectLifeCycle() != null && omsAccountSubject.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAccountSubject.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAccountSubject.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAccountSubject != null && omsAccountSubject.getSubjectLifeCycle() != null && omsAccountSubject.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAccountSubject.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAccountSubject != null && omsAccountSubject.getSubjectLifeCycle() != null && omsAccountSubject.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAccountSubject.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAccountSubject);
    }
}
